from selenium import webdriver
import datetime
import time

def login(): 
  
  browser.get("http://www.taobao.com")
  time.sleep(3)
  if browser.find_element_by_link_text("亲，请登录"):
    browser.find_element_by_link_text("亲，请登录").click()
    print("请在15秒内完成扫码")
    time.sleep(3)
    browser.get("https://cart.taobao.com/cart.htm")
    time.sleep(3)
    now = datetime.datetime.now()
    print('login success', now.strftime('%Y-%m-%d %H:%M:%S'))

def buy(times):
  while True:
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    # 对比时间， 时间到了的话点击结算
    if now > times:
      while True:
          if browser.find_element_by_id('J_SelectAll2'):
            browser.find_element_by_id('J_SelectAll2').click()
            time.sleep(1)
          if browser.find_element_by_link_text("结 算"):
            browser.find_element_by_link_text("结 算").click()
            print("结算成功")
          if browser.find_element_by_link_text("提交订单"):
            browser.find_element_by_link_text("提交订单").click()
            now1 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            print('抢购成功时间：%s', now1)

if __name__ == '__main__':
  times = input("请输入抢购时间，格式如(2020-02-18 11:20:00.000000):")
  options = webdriver.ChromeOptions()
  options.add_argument('lang=zh_CN.UTF-8')
  options.add_argument(
      'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"')
  browser = webdriver.Chrome(options=options)
  browser.maximize_window()
  login()
  buy(times)
    
